package com.wly.datapool.tower
{
	import com.wly.ActiveManager;
	import com.wly.PanelManager;
	import com.wly.datapool.bullet.BulletInfo;
	import com.wly.datapool.define.GlobalDefine;
	import com.wly.datapool.global.GlobalInfo;
	import com.wly.datapool.monster.MonsterInfo;
	import com.wly.dynamicc.DynamicManager;
	import com.wly.dynamicc.DynamicObject;
	import com.wly.ui.panel.GlobalInfoPanel;
	import com.wly.ui.panel.TowerInfoPanel;
	import com.wly.ui.sound.SoundLayer;
	import com.wly.utils.MyPoint;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	public class TowerInfo
	{
		public var baseInfo:TowerStatic;
		public var activeKey:int;
		public var model:MovieClip;
		private var _attackFactor:Number = 1;
		public var attackSound:MovieClip;
		private var _attackAreaFactor:Number = 1;
		public var bulletIndex:int;
		private var _expCur:int;
		private var _expMax:int = 300;
		private var _targetMonster:MonsterInfo;
		public var timeCount:int;
		private var _infoPanel:TowerInfoPanel;
		
		private var _dynamicObj:DynamicObject;
		
		public function TowerInfo(baseIndex:int):void
		{
			baseInfo = TowerPool.getInstance().getTowerByIndex(baseIndex);
			trace(this, baseInfo);
		}
		
		public function create():Boolean
		{			
			if(GlobalInfo.money>=baseInfo.createPrice)
			{
				GlobalInfo.money -= baseInfo.createPrice;
				GlobalInfoPanel.getInstance().upodateMoney();
			}
			else
			{
				trace(this, "create tower failed not enough money ^_^!");
				return false;
			}
			model = new baseInfo.modelClass;
			_dynamicObj = new DynamicObject();
			_dynamicObj.action = onTick;
			model.addEventListener(MouseEvent.CLICK, onClick);
			DynamicManager.getInstance().registerObj(_dynamicObj);
			return true;
		}
		
		public function destory():void
		{
			if(_infoPanel)
			{
				destortPanel();
			}
			model.removeEventListener(MouseEvent.CLICK, onClick);
			DynamicManager.getInstance().unRegisterObj(_dynamicObj);
			ActiveManager.getInstance().removeTower(this);
		}
		
		public function onClick(e:MouseEvent):void
		{
			_infoPanel = new TowerInfoPanel;
			_infoPanel.updateData(this);
			PanelManager.getInstance().showPanel(_infoPanel,x,y);
			e.stopPropagation();
		}
		
		public function set targetMonster(monster:MonsterInfo):void
		{
			_targetMonster = monster;
			if(timeCount>baseInfo.speed)
			{
				timeCount = 0;
			}
		}
		
		public function get targetMonster():MonsterInfo
		{
			return _targetMonster;
		}
		
		public function onTick():void
		{
			timeCount++;
			if(_targetMonster)
			{
				//if(!_targetMonster.isAlive)
				
				if(!_targetMonster.isAlive)
				{
					_targetMonster = null;
					return;
				}
				
				var dis:int = MyPoint.distance(new MyPoint(model.x, model.y), new MyPoint(_targetMonster.x, _targetMonster.y));
				if(dis>attackArea)
				{
					_targetMonster = null;
					return;
				}
				
				if(timeCount%baseInfo.speed == 0)
				{
					var bullet:BulletInfo = new BulletInfo(bulletIndex);
					bullet.speed  = 5;
					bullet.attack = attack;
					bullet.model.x = this.model.x;
					bullet.model.y = this.model.y;
					ActiveManager.getInstance().addBullet(bullet);
					bullet.targetMonster = _targetMonster;
					bullet.start();
					curExp += 20;
					if(GlobalInfo.isPlaySound)
					{
						attackSound = new baseInfo.attackSound;
						SoundLayer.getInstance().addChild(attackSound);
						attackSound.gotoAndStop(2);
					}
					
					if(_infoPanel)
					{
						_infoPanel.updateData(this);
					}
					timeCount = 0;
				}
			}
		}
		
		public function get hasMonster():Boolean
		{
			if(_targetMonster && _targetMonster.isAlive)
			{
				return true;
			}
			return false;
		}
		
		public function destortPanel():void
		{
			if(_infoPanel)
			{
				_infoPanel.destory();
				PanelManager.getInstance().removePanel(_infoPanel);
				_infoPanel = null;
			}
		}
		
		public function get x():int
		{
			if(model)
			{
				return model.x;
			}
			return 0;
		}
		
		public function get y():int
		{
			if(model)
			{
				return model.y;
			}
			return 0;
		}
		
		public function set attackFactor(factor:Number):void
		{
			_attackFactor = factor;
		}
			
		public function get attack():int
		{
			return baseInfo.baseAttack*_attackFactor;
		}
		
		public function get level():int
		{
			return baseInfo.level;
		}
		
		public function set attackAreaFactor(val:Number):void
		{
			_attackAreaFactor = val;
		}
		
		public function get attackArea():int
		{
			return baseInfo.attackArea*_attackAreaFactor;
		}
		
		public function get speed():int
		{
			return baseInfo.speed;
		}
		
		public function set curExp(val:int):void
		{
			_expCur = val;
		}
		
		public function get curExp():int
		{
			return _expCur;
		}
		
		public function get maxExp():int
		{
			return _expMax;
		}
		
		public function doSell():void
		{
			GlobalInfo.money += baseInfo.sellPrice;
			GlobalInfoPanel.getInstance().upodateMoney();
			destory();
		}
		
		public function doUpgrade():void
		{
			trace(this, "doupgrade");
			if(!baseInfo.nextTower)
			{
				trace(this, "topgrade");
				return;
			}
			
			if(curExp<=maxExp)
			{
				trace(this, "exp is not enough");
				return;
			}
			
			var tower:TowerInfo = TowerPool.getInstance().createTower(baseInfo.nextTower);
			if(tower)
			{
				tower.model.x = x;
				tower.model.y = y;
				tower.curExp = curExp-maxExp;
				destory();
				ActiveManager.getInstance().addTower(tower);
			}
			
		}
	}
}